#!/bin/bash

# MySQL 安装脚本
# 需要 root 权限执行

set -e

MYSQL_TAR="/home/askhar/Downloads/mysql-9.5.0-linux-glibc2.17-x86_64-minimal.tar.xz"
MYSQL_DIR="/usr/local/mysql"
MYSQL_DATA_DIR="/var/lib/mysql"
MYSQL_USER="mysql"
MYSQL_GROUP="mysql"

echo "开始安装 MySQL 9.5.0..."

# 检查是否以 root 权限运行
if [ "$EUID" -ne 0 ]; then 
    echo "错误: 请使用 sudo 运行此脚本"
    echo "使用方法: sudo bash install_mysql.sh"
    exit 1
fi

# 1. 检查文件是否存在
if [ ! -f "$MYSQL_TAR" ]; then
    echo "错误: 找不到文件 $MYSQL_TAR"
    exit 1
fi

# 2. 创建 MySQL 用户和组（如果不存在）
if ! id "$MYSQL_USER" &>/dev/null; then
    echo "创建 MySQL 用户和组..."
    groupadd -r $MYSQL_GROUP
    useradd -r -g $MYSQL_GROUP -s /bin/false $MYSQL_USER
fi

# 3. 解压 MySQL
echo "解压 MySQL 文件..."
cd /tmp
rm -rf mysql-9.5.0-linux-glibc2.17-x86_64-minimal
tar -xf "$MYSQL_TAR"

# 4. 移动 MySQL 到安装目录
echo "安装 MySQL 到 $MYSQL_DIR..."
if [ -d "$MYSQL_DIR" ]; then
    echo "警告: $MYSQL_DIR 已存在，将备份为 ${MYSQL_DIR}.backup"
    mv "$MYSQL_DIR" "${MYSQL_DIR}.backup.$(date +%Y%m%d_%H%M%S)"
fi
mv mysql-9.5.0-linux-glibc2.17-x86_64-minimal "$MYSQL_DIR"

# 5. 创建数据目录
echo "创建数据目录..."
mkdir -p "$MYSQL_DATA_DIR"
chown -R $MYSQL_USER:$MYSQL_GROUP "$MYSQL_DATA_DIR"

# 6. 设置 MySQL 目录权限
echo "设置目录权限..."
chown -R $MYSQL_USER:$MYSQL_GROUP "$MYSQL_DIR"

# 7. 初始化数据库
echo "初始化 MySQL 数据库..."
cd "$MYSQL_DIR"
bin/mysqld --initialize-insecure --user=$MYSQL_USER --datadir="$MYSQL_DATA_DIR"

# 8. 创建 MySQL 配置文件
echo "创建 MySQL 配置文件..."
cat > /etc/my.cnf <<EOF
[mysqld]
datadir=$MYSQL_DATA_DIR
socket=/var/run/mysqld/mysqld.sock
user=$MYSQL_USER
port=3306
bind-address=127.0.0.1

[mysql]
socket=/var/run/mysqld/mysqld.sock

[client]
socket=/var/run/mysqld/mysqld.sock
EOF

# 9. 创建 socket 目录
mkdir -p /var/run/mysqld
chown -R $MYSQL_USER:$MYSQL_GROUP /var/run/mysqld

# 10. 创建 systemd 服务文件
echo "创建 systemd 服务..."
cat > /etc/systemd/system/mysql.service <<EOF
[Unit]
Description=MySQL Server
After=network.target

[Service]
Type=notify
User=$MYSQL_USER
Group=$MYSQL_GROUP
ExecStart=$MYSQL_DIR/bin/mysqld --defaults-file=/etc/my.cnf
ExecReload=/bin/kill -HUP \$MAINPID
Restart=on-failure
RestartSec=5
TimeoutSec=0

[Install]
WantedBy=multi-user.target
EOF

# 11. 添加到 PATH
echo "添加到 PATH..."
if ! grep -q "$MYSQL_DIR/bin" /etc/profile; then
    echo "export PATH=\$PATH:$MYSQL_DIR/bin" >> /etc/profile
fi

# 12. 创建符号链接
ln -sf "$MYSQL_DIR/bin/mysql" /usr/local/bin/mysql
ln -sf "$MYSQL_DIR/bin/mysqld" /usr/local/bin/mysqld
ln -sf "$MYSQL_DIR/bin/mysqldump" /usr/local/bin/mysqldump
ln -sf "$MYSQL_DIR/bin/mysqladmin" /usr/local/bin/mysqladmin

# 13. 重新加载 systemd
systemctl daemon-reload

# 14. 启动 MySQL 服务
echo "启动 MySQL 服务..."
systemctl enable mysql
systemctl start mysql

# 15. 等待 MySQL 启动
sleep 3

# 16. 设置 root 密码
echo "设置 root 密码..."
$MYSQL_DIR/bin/mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';" 2>/dev/null || \
$MYSQL_DIR/bin/mysql -u root -e "SET PASSWORD FOR 'root'@'localhost' = 'root';" 2>/dev/null || \
echo "注意: 可能需要手动设置 root 密码"

echo ""
echo "=========================================="
echo "MySQL 安装完成！"
echo "=========================================="
echo "安装目录: $MYSQL_DIR"
echo "数据目录: $MYSQL_DATA_DIR"
echo "配置文件: /etc/my.cnf"
echo ""
echo "启动服务: sudo systemctl start mysql"
echo "停止服务: sudo systemctl stop mysql"
echo "查看状态: sudo systemctl status mysql"
echo ""
echo "连接 MySQL:"
echo "  mysql -u root -p"
echo "  密码: root"
echo ""
echo "创建项目数据库:"
echo "  mysql -u root -proot -e \"CREATE DATABASE IF NOT EXISTS dukan CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\""
echo "=========================================="

